clear

import excel "insurance_database.xlsx", sheet("database") firstrow

drop insexp1playerdislike participantcode participantlabel participant_index_in_pages participant_max_page_index participant_current_app_name participant_current_page_name participanttime_started_utc participantvisited participantmturk_worker_id participantmturk_assignment_id insexp1playerbackcount participantpayoff participant_is_bot sessioncode sessionlabel sessionmturk_HITId sessionmturk_HITGroupId sessioncomment sessionis_demo sessionconfigreal_world_curr* sessionconfigparticipation_fee sessionconfigname insexp1playerid_in_group insexp1playerrole insexp1playerpayoff insexp1playerscore_1 insexp1playerwtp insexp1playerwtpconfirm insexp1groupid_in_subsession insexp1subsessionround_number insexp1playerq1_*

* Task 1

rename participantid_in_session id
rename insexp1playerdtable_1 risk1
rename insexp1playerdtable_2 risk2
rename insexp1playerdtable_3 risk3
rename insexp1playerdtable_4 risk4
rename insexp1playerdtable_5 risk5
rename insexp1playerdtable_6 risk6
rename insexp1playerdtable_7 risk7
rename insexp1playerdtable_8 risk8
rename insexp1playerdtable_9 risk9
rename insexp1playerdtable_10 risk10

* single switch
gen one_risk_switch=0
replace one_risk_switch=1 if risk10>=risk9 & risk9>=risk8 & risk8>=risk7 & risk7 >=risk6 & risk6>=risk5 & risk5>=risk4 & risk4>=risk3 & risk3>=risk2 & risk2>=risk1
label variable one_risk_switch "single risk switch point"

* switching decision (11=risk-averse option for all 10 decisions)
gen risk_switch=. if one_risk_switch==0
replace risk_switch=1 if risk1==1 & one_risk_switch==1
replace risk_switch=2 if risk1==0 & risk2==1 & one_risk_switch==1
replace risk_switch=3 if risk1==0 & risk2==0 & risk3==1 & one_risk_switch==1
replace risk_switch=4 if risk1==0 & risk2==0 & risk3==0 & risk4==1 & one_risk_switch==1
replace risk_switch=5 if risk1==0 & risk2==0 & risk3==0 & risk4==0 & risk5==1 & one_risk_switch==1
replace risk_switch=6 if risk1==0 & risk2==0 & risk3==0 & risk4==0 & risk5==0 & risk6==1 & one_risk_switch==1
replace risk_switch=7 if risk1==0 & risk2==0 & risk3==0 & risk4==0 & risk5==0 & risk6==0 & risk7==1 & one_risk_switch==1
replace risk_switch=8 if risk1==0 & risk2==0 & risk3==0 & risk4==0 & risk5==0 & risk6==0 & risk7==0 & risk8==1 & one_risk_switch==1
replace risk_switch=9 if risk1==0 & risk2==0 & risk3==0 & risk4==0 & risk5==0 & risk6==0 & risk7==0 & risk8==0 & risk9==1 & one_risk_switch==1
replace risk_switch=10 if risk1==0 & risk2==0 & risk3==0 & risk4==0 & risk5==0 & risk6==0 & risk7==0 & risk8==0 & risk9==0 & risk10==1 & one_risk_switch==1
replace risk_switch=11 if risk1==0 & risk2==0 & risk3==0 & risk4==0 & risk5==0 & risk6==0 & risk7==0 & risk8==0 & risk9==0 & risk10==0 & one_risk_switch==1
label variable risk_switch "switching lottery for single switchers"

gen risky_choices = risk1 + risk2 + risk3 + risk4 + risk5 + risk6 + risk7 + risk8 + risk9 + risk10
label variable risky_choices "number of risky choices"

* Task 2

* quiz responses (to earn $20)
rename insexp1playerq2_1 quizQ1
rename insexp1playerq2_2 quizQ2
rename insexp1playerq2_3_R quizQ3
rename insexp1playerq2_4 quizQ4
rename insexp1playerq2_5 quizQ5
rename insexp1playerq2_6 quizQ6
rename insexp1playerq2_7 quizQ7
rename insexp1playerq2_8 quizQ8
rename insexp1playerq2_9 quizQ9
rename insexp1playerq2_10 quizQ10
rename insexp1playerq2_11_R quizQ11
rename insexp1playerq2_12 quizQ12
rename insexp1playerq2_13 quizQ13
rename insexp1playerscore_2 quiz_score

gen pass_quiz=0
replace pass_quiz=1 if quiz_score>4
label variable pass_quiz "pass quiz & proceed to Task 2"

* test of understanding question responses
rename insexp1playerqwtp_1 testQ1
rename insexp1playerqwtp_2 testQ2
rename insexp1playerqwtp_3 testQ3
rename insexp1playerqwtp_4 testQ4

* WTP for insurance premia: $1, $5, $9, $13
rename insexp1playerwtp_1 wtp_ins1
rename insexp1playerwtp_5 wtp_ins5
rename insexp1playerwtp_9 wtp_ins9
rename insexp1playerwtp_13 wtp_ins13
rename insexp1playerwtp_score test_score
move test_score wtp_ins1

rename insexp1playerrealins premium
label variable premium "premium drawn"

rename insexp1playergetins insured
replace insured=. if quiz_score==4
label variable insured "indicator for purchase of insurance"

rename insexp1playerroundedfinalpay* finalpayoff

* Questionnaire 
rename insexp1playergender gender
rename insexp1playerage age
	label variable age "age"

rename insexp1playerfield_of_study major
rename insexp1playeryear_of_study year_study

rename insexp1playerresent resent
rename insexp1playerrebel rebel
rename insexp1playercourteous courteous
rename insexp1playerjealous jealous
rename insexp1playerirritated irritated
rename insexp1playersneak sneak
rename insexp1playerlikeme likeme

rename insexp1playerclear_instruc clear_instr
	label variable clear_instr "clear instructions {1,2,3,4,5,6,7}"
rename insexp1playerrealization realize_cheat
	label variable realize_cheat "did you realize you could report a loss different from actual loss?"

rename insexp1playert2insurance buy_ins
rename insexp1playerno_insurance_reg noins_regret
	bysort insured: tab noins_regret
rename insexp1playerinsurance_estima dishonest_estimate
label variable dishonest_estimate "Out of 10 insured in experiment, how many would report dishonestly to earn more money?"

rename insexp1playerearned_statement earned
rename insexp1playerinsurance_purcha purchase_realins
label variable purchase_realins "have you ever purchased insurance?"
rename insexp1playertravelins purchase_travel_ins
rename insexp1playermedicalins purchase_medical_ins
rename insexp1playercarins purchase_car_ins
rename insexp1playerrentersins purchase_renter_ins
rename insexp1playerpropertyins purchase_property_ins
rename insexp1playerlifeins purchase_life_ins
rename insexp1playerotherins purchase_other_ins
rename insexp1playernoins purchase_no_ins
rename insexp1playerinsurance_other purchase_other_type

replace purchase_travel_ins=0 if purchase_travel_ins!=1
replace purchase_medical_ins=0 if purchase_medical_ins!=1
replace purchase_car_ins=0 if purchase_car_ins!=1
replace purchase_renter_ins=0 if purchase_renter_ins!=1
replace purchase_property_ins=0 if purchase_property_ins!=1
replace purchase_life_ins=0 if purchase_life_ins!=1
replace purchase_other_ins=0 if purchase_other_ins!=1
replace purchase_no_ins=0 if purchase_no_ins!=1
label variable purchase_no_ins "never purchased insurance"
replace purchase_realins=1-purchase_no_ins

rename insexp1playerclaim_history claim
replace claim="0" if claim=="No"
replace claim="1" if claim=="Yes"
destring claim, replace
label variable claim "Have you ever filed an insurance claim?"

rename insexp1playercheat cheat_ins_freq
rename insexp1playercomments comments

foreach v of varlist wtp_ins1-wtp_ins13 {
		 replace `v'=1-`v' 
	}

foreach v of varlist wtp_ins1-wtp_ins13 {
		 label variable `v' "indicator v. for WTP price `v'" 
	}

	
**********
* GENERATE
**********

gen overallid=100*session+id
move overallid resent
label variable overallid "100*session+id"

* monotonic WTP
gen mono_wtp=0
replace mono_wtp=. if wtp_ins1==. & wtp_ins5==. & wtp_ins9==. & wtp_ins13==.
replace mono_wtp=1 if wtp_ins1 <= wtp_ins5 & wtp_ins5 <= wtp_ins9 & wtp_ins9<= wtp_ins13
label variable mono_wtp "monotonic WTP"

* max WTP for insurance
gen max_wtp=0 if wtp_ins1 ==0 &  wtp_ins5 == 0 & wtp_ins9==0 & wtp_ins13 == 0
replace max_wtp=1 if wtp_ins1==1 & wtp_ins5==0 & wtp_ins9==0 & wtp_ins13 == 0
replace max_wtp=5 if wtp_ins1==1 & wtp_ins5==1 & wtp_ins9==0 & wtp_ins13 == 0
replace max_wtp=9 if wtp_ins1==1 & wtp_ins5==1 & wtp_ins9==1 & wtp_ins13 == 0
replace max_wtp=13 if wtp_ins1==1 & wtp_ins5==1 & wtp_ins9==1 & wtp_ins13 == 1
label variable max_wtp "max wtp for insurance"

* final reported loss variable (after any revisions)
gen loss_report=insexp1playerloss1 if insexp1playerlossredo1==0
replace loss_report=insexp1playerloss2 if insexp1playerlossredo1==1 & insexp1playerlossredo2==0
replace loss_report=insexp1playerloss3 if insexp1playerlossredo2==1 & insexp1playerlossredo3==0
replace loss_report=insexp1playerloss4 if insexp1playerlossredo3==1
label variable loss_report "reported loss"

* final reported contents of payment container
gen container=insexp1playercontainer1 if insexp1playerlossredo1==0
replace container=insexp1playercontainer2 if insexp1playerlossredo1==1 & insexp1playerlossredo2==0
replace container=insexp1playercontainer3 if insexp1playerlossredo2==1 & insexp1playerlossredo3==0
replace container=insexp1playercontainer4 if insexp1playerlossredo3==1 
label variable container "final reported contents of container"

*** Questionnaire

* realize cheat as binary variable 
gen know_cheat=0 if realize_cheat=="No"
replace know_cheat=1 if realize_cheat=="Yes"
bysort insured: su know_cheat
label variable know_cheat "did you realize you could report a different loss?"
move know_cheat realize_cheat

* social desirability bias index
gen sdb5=(1-resent)+(1-rebel)+courteous+(1-jealous)+(1-irritated)
label variable sdb5 "social desirability 5Q index"

gen sdb7=(1-resent)+(1-rebel)+courteous+(1-jealous)+(1-irritated)+(1-sneak)+likeme
label variable sdb7 "social desirability 7Q index"

gen age2 = age*age
label variable age2 "age squared"

gen female=0
replace female=1 if gender=="Female"
label variable female "female=1, otherwise=0"

rename insexp1playergender2 selfreport_gender

*** Task 2

generate max5prem15=0
replace max5prem15=1 if max_wtp==5 & (premium<9)
label variable max5prem15 "max_wtp=5 & premium=1 | 5"

generate max59prem5=0
replace max59prem5=1 if premium==5 & (max_wtp==5 | max_wtp==9)
label variable max59prem5 "max_wtp = 5 | 9 & premium=5"


gen cs=.
* replace cs=0 if max_wtp==premium if insured==1
replace cs=max_wtp-premium if insured==1 
label variable cs "consumer surplus from insurance; uninsured=."

gen cs0=.
replace cs0=0 if insured==1 & cs>0
replace cs0=1 if insured==1 & cs==0 
label variable cs0 "CS=0, or CS>0; uninsured=."

gen subj=22 if session<4
replace subj=21 if session==4
replace subj=19 if session>4 & session<10
replace subj=15 if session==10
replace subj=18 if session==11
replace subj=17 if session==12
label variable subj "number of subjects that participated in Part 2"

* insured interacted w/ premium paid 

gen ins_prem1=0 
replace ins_prem1=1 if insured==1 & premium==1

gen ins_prem5=0 
replace ins_prem5=1 if insured==1 & premium==5

gen ins_prem9=0 
replace ins_prem9=1 if insured==1 & premium==9

gen ins_prem13=0 
replace ins_prem13=1 if insured==1 & premium==13

* insured interacted w/ max wtp for insurance

gen ins_max_wtp1=0
replace ins_max_wtp1=1 if insured==1 & max_wtp==1

gen ins_max_wtp5=0
replace ins_max_wtp5=1 if insured==1 & max_wtp==5

gen ins_max_wtp9=0
replace ins_max_wtp9=1 if insured==1 & max_wtp==9

gen ins_max_wtp13=0
replace ins_max_wtp13=1 if insured==1 & max_wtp==13

foreach v of varlist ins_prem1-ins_prem13 {
		 label variable `v' "indicator for assigned premium" 
	}
	
	foreach v of varlist ins_max_wtp1-ins_max_wtp13 {
		 label variable `v' "indicator for max WTP for insurance" 
	}

gen ins_prem913=0
replace ins_prem913=1 if ins_prem9==1 | ins_prem13==1
label variable ins_prem913 "indicator assigned premium 9 or 13"
	
gen leftover_sum=leftover6*6+leftover8*8+leftover10*10+leftover12*12+leftover20*20
tab subj leftover_sum
label variable leftover_sum "sum of losses in unchosen tins"

gen mean_true_loss=((6*5+8*5+10*5+12*5+20*5)-leftover_sum)/25 if session!=12
replace mean_true_loss=((6*4+8*4+10*4+12*4+20*4)-leftover_sum)/20 if session==12
label variable mean_true_loss "mean available loss based on actual distr. of tins"

gen excess_loss=loss_report-mean_true_loss
label variable excess_loss "reported loss - mean true loss"

label define insured 0 "uninsured" 1 "insured", replace
label values insured insured


************
* HISTOGRAMS
************

* Figure 2
histogram loss_report, discrete fraction xlabel(0 8 10 12 14) by(insured) 
* title("Figure 2 - Histograms of reported losses by insured status")

* Figure 3
histogram loss_report if insured==1 & premium<13, discrete fraction xlabel(0 8 10 12 14) by(premium, row(1))


**********
* ANALYSIS
**********

* insured, $14 loss
bitesti 174 45 0.2
* insured, $0 loss
bitesti 174 28 0.2
* uninsured, $14 loss
bitesti 58 17 0.2


*** RISK PREFERENCES
tab risk_switch if one_risk_switch==1 & pass_quiz==1
su risk_switch if one_risk_switch==1 & pass_quiz==1, detail
tab risky_choices if pass_quiz==1
su risky_choices if pass_quiz==1
tab risky_choices if pass_quiz==1 & one_risk_switch==1 

* Table
bysort max_wtp premium: su loss_report
bysort max_wtp premium: su excess_loss

* insured vs. uninsured
ranksum loss_report if pass_quiz==1, by(insured)
disp 58/232
ranksum excess_loss if pass_quiz==1, by(insured)
bysort insured: su loss_report, detail
bysort insured: su excess_loss
ranksum loss_report, by(insured) exact

* entitlement effect
* among those w/ max WTP=5 & premium 1 | 5
ranksum excess_loss if max5prem15==1, by(premium)
* among those w/ max WTP= 5 | 9 & premium = 1
ranksum excess_loss if max59prem5==1, by(max_wtp)

* do risk preferences predict wtp for insurance?
* NO!
reg max_wtp risky_choices if one_risk_switch, robust
reg max_wtp risky_choices, robust
reg max_wtp risky_choices if clear_instr>5, robust

bysort cs0: su loss_report

* observe the number of subjects who reported a smaller loss than their insurance payment 
* 33/174 insured ss report loss than their premium
tab premium loss_report if insured==1

* among insured, reporting by premium: test for entitlement effect
bysort premium: su loss_report if insured==1
bysort premium: tab loss_report if insured==1

bysort premium: su excess_loss if insured==1

* reporting by insured status
bysort insured: su loss_report
bysort insured: su excess_loss
ranksum excess_loss, by(insured)
ttest excess_loss, by(insured) unequal
ranksum excess_loss, by(insured) 

* (3)
reg excess_loss ins_prem5 ins_prem913 ins_max_wtp5 ins_max_wtp9 ins_max_wtp13 if insured==1, robust
test ins_prem5=ins_prem913
* confidence interval analysis
* $5 premium
disp -.533+1.992
disp -.533+1.992 + 8.80
disp -.533-0.928
* $9 premiumd
disp -.533 + 1.417 + 8.80

* (4)
reg excess_loss ins_prem5 ins_prem913 ins_max_wtp5 ins_max_wtp9 ins_max_wtp13 age female risky_choices sdb5 purchase_realins claim if insured==1, robust
test ins_prem5=ins_prem913


*********
* RESHAPE
*********

* reshape for WTP for insurance regressions
reshape long wtp_ins, i(overallid) j(price)
* (1)
reg wtp_ins i.price risky_choices, cluster(overallid)
logit wtp_ins i.price risky_choices, cluster(overallid)

* (2) 
reg wtp_ins i.price risky_choices age female purchase_realins claim, cluster(overallid)
logit wtp_ins i.price risky_choices age female purchase_realins claim, cluster(overallid)
